package org.dftproject.genesis.ui.pages.pedigree.tree;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

public class OneToManyConnection extends AlphaFigure {
	
	public static final Color lineColor = new Color(null, 0x88, 0x88, 0x88);
	
	private IFigure source;
	private final LinkedList<IFigure> targets = new LinkedList<IFigure>();
	
	private final int verticalBarWidth;
	
	public OneToManyConnection() {
		this.verticalBarWidth = 3;
	}
	
	public OneToManyConnection(int verticalBarWidth) {
		this.verticalBarWidth = verticalBarWidth;
	}
	
	public IFigure getSourceAnchor() {
		return source;
	}
	
	public void setSourceAnchor(IFigure source) {
		this.source = source;
	}
	
	public List<IFigure> getTargetAnchors() {
		return Collections.unmodifiableList(targets);
	}
	
	public void addTargetAnchor(IFigure target) {
		targets.add(target);
	}
	
	@Override
	public void layout() {
		int count = targets.size();
		if (count == 1) {
			IFigure target = targets.getFirst();
			Point p1 = new Point(source.getBounds().right(), source.getBounds().getCenter().y);
			Point p2 = new Point(target.getBounds().x, target.getBounds().getCenter().y);
			setBounds(new Rectangle(p1, p2));
		} else if (count > 1) {
			int xStart = source.getBounds().right() - 1;
			int yStart = source.getBounds().getCenter().y;

			int xEnd = targets.getFirst().getBounds().x;

			int yEndMin = targets.getFirst().getBounds().getCenter().y;
			int yEndMax = targets.getLast().getBounds().getCenter().y;

			setBounds(new Rectangle(xStart, Math.min(yEndMin, yStart), xEnd - xStart, Math.max(yEndMax, yStart) - Math.min(yEndMin, yStart) + 1));
		}
	}
	
	@Override
	protected void paintFigure(Graphics graphics) {
		graphics.pushState();
		graphics.setAntialias(SWT.ON);
		
		int count = targets.size();
		if (count == 1) {
			IFigure target = targets.getFirst();
			Point p1 = new Point(source.getBounds().right(), source.getBounds().getCenter().y);
			Point p2 = new Point(target.getBounds().x, target.getBounds().getCenter().y);

			graphics.setForegroundColor(lineColor);
			graphics.drawLine(p1, p2);
		} else if (count > 1) {
			int xStart = source.getBounds().right() - 1;
			int yStart = source.getBounds().getCenter().y;

			int xEnd = targets.getFirst().getBounds().x;
			int xMiddle = xEnd - 9;
			if (verticalBarWidth == 1)
				xMiddle = xStart + (xEnd - xStart) / 2;

			int yEndMin = targets.getFirst().getBounds().getCenter().y;
			int yEndMax = targets.getLast().getBounds().getCenter().y;

			int yMiddle = yEndMin + (yEndMax - yEndMin) / 2;

			graphics.setForegroundColor(lineColor);
			graphics.setBackgroundColor(lineColor);
			graphics.drawLine(xStart, yStart, xMiddle, yMiddle);

			for (IFigure target : targets) {
				int y = target.getBounds().getCenter().y;
				graphics.drawLine(xMiddle + verticalBarWidth - 1, y, xEnd, y);
			}

			graphics.drawLine(xMiddle + verticalBarWidth - 1, yEndMin, xMiddle + verticalBarWidth - 1, yEndMax);
			if (verticalBarWidth > 1) {
				graphics.pushState();
				graphics.clipRect(new Rectangle(xMiddle, yEndMin, verticalBarWidth - 1, yEndMax - yEndMin + 1));
				graphics.fillRoundRectangle(new Rectangle(xMiddle, yEndMin, Math.max(verticalBarWidth * 2, 6), yEndMax - yEndMin + 1), 3, 3);
				graphics.popState();
			}
		}
		
		graphics.popState();
	}

}
